package cn.s1995.sloth.bizs.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.hutool.core.util.ObjectUtil;
import cn.s1995.sloth.bizs.entity.ProdGroup;
import cn.s1995.sloth.bizs.entity.ProdGroupReference;
import cn.s1995.sloth.bizs.mapper.ProdGroupMapper;
import cn.s1995.sloth.bizs.service.ProdGroupReferenceService;
import cn.s1995.sloth.bizs.service.ProdGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * 产品分组
 *
 * @author Sashi
 * @date 2023-12-20 09:49:45
 */
@Service
public class ProdGroupServiceImpl extends ServiceImpl<ProdGroupMapper, ProdGroup> implements ProdGroupService {

	@Autowired
	private ProdGroupReferenceService prodGroupReferenceService;

	@Override
	public void updateProdCount(Long id) {
		if(ObjectUtil.isNotNull(id)) {
			ProdGroup prodGroup = this.getById(id);
			if(ObjectUtil.isNotNull(prodGroup)) {
		        long count = prodGroupReferenceService.count(
		        		Wrappers.lambdaQuery(ProdGroupReference.class)
		        			.eq(ProdGroupReference::getGroupId, id)
		        );
		        prodGroup.setProdCount((int)count);
		        this.updateById(prodGroup);
			}
		}

	}
}

